package com.losdun.aftersales.manager;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.losdun.aftersales.domain.ContractOrder;
import com.losdun.aftersales.domain.Install;
import com.losdun.aftersales.domain.Repair;
import com.losdun.aftersales.enums.AftersalesTypeEnum;
import com.losdun.aftersales.service.IContractOrderService;
import com.losdun.common.core.domain.entity.SysUser;
import com.losdun.common.sms.AliyunSmsClient;
import com.losdun.common.utils.DateUtils;
import com.losdun.system.service.ISysUserService;
import com.losdun.weixin.common.WxConstants;
import com.losdun.weixin.manager.MessageManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @Description
 * @Author Sword.Ye
 * @Date 2025/11/21
 **/
@Component
@Slf4j
public class NoticeManager {

    @Autowired
    private AliyunSmsClient aliyunSmsClient;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IContractOrderService orderService;
    /**
     * 预约安装成功发送通知
     * @param
     */
    @Async("asyncExecutor")
    public void bookInstallSuccessSendNotice(ContractOrder order, Install install){
        try {
            //小程序通知客户
            log.info("预约安装小程序通知客户！openid:{}",install.getOpenId());
            MessageManager.sendMessage(WxConstants.SUBSCRIBE_MSG_ID_BOOK_SUCCESS,install.getOpenId(),
                    MessageManager.buildBookSuccessMsgData(install.getContacts(),install.getContactNumber(), DateUtils.formatDate(install.getInstallTime()),install.getAddress(), AftersalesTypeEnum.getLabelByValue(install.getType())));
        } catch (Exception e) {
            log.error("预约安装小程序通知发送失败！{}",e.getMessage(),e);
        }
        try {
            //短信通知客户
            log.info("预约安装短信发送客户！{}",order.getMobile());
            JSONObject msgData=new JSONObject();
            msgData.put("type",AftersalesTypeEnum.getLabelByValue(install.getType()));
            aliyunSmsClient.sendSmsByType("customerFive",order.getMobile(), JSON.toJSONString(msgData));
        } catch (Exception e) {
            log.error("预约安装短信发送客户失败！{}",e.getMessage(),e);
        }

        try {
            //短信通知销售
            SysUser sales = getSysUser(order.getSalesmanId());
            if(sales!=null) {
                log.info("预约安装短信发送销售！{}",sales.getPhonenumber());
                JSONObject msgData = new JSONObject();
                msgData.put("salesName", order.getSalesman());
                msgData.put("customerName", order.getUserName());
                msgData.put("type", AftersalesTypeEnum.getLabelByValue(install.getType()));
                aliyunSmsClient.sendSmsByType("salesThree", sales.getPhonenumber(), JSON.toJSONString(msgData));
            }
        } catch (Exception e) {
            log.error("预约安装短信发送销售失败！{}",e.getMessage(),e);
        }
    }

    /**
     * 预约售后成功发送通知
     * @param order
     * @param repair
     */
    @Async("asyncExecutor")
    public void bookRepairSuccessSendNotice(ContractOrder order, Repair repair){
        try {
            //小程序通知客户
            log.info("预约售后成功小程序通知客户！openid:{}",repair.getOpenId());
            MessageManager.sendMessage(WxConstants.SUBSCRIBE_MSG_ID_BOOK_SUCCESS,repair.getOpenId(),
                    MessageManager.buildBookSuccessMsgData(repair.getContacts(),repair.getContactNumber(), DateUtils.nowDateTimeStr(),repair.getAddress(), AftersalesTypeEnum.getLabelByValue(repair.getType())));
        } catch (Exception e) {
            log.error("预约售后小程序通知客户发送失败！{}",e.getMessage(),e);
        }
        try {
            //短信通知客户
            log.info("预约售后成功短信发送客户！{}",order.getMobile());
            JSONObject msgData=new JSONObject();
            msgData.put("type",AftersalesTypeEnum.getLabelByValue(repair.getType()));
            aliyunSmsClient.sendSmsByType("customerFive",order.getMobile(), JSON.toJSONString(msgData));
        } catch (Exception e) {
            log.error("预约售后短信发送客户失败！{}",e.getMessage(),e);
        }

        try {
            //短信通知销售
            SysUser sales = getSysUser(order.getSalesmanId());
            if(sales!=null) {
                log.info("预约售后成功短信发送销售！{}",sales.getPhonenumber());
                JSONObject msgData = new JSONObject();
                msgData.put("salesName", order.getSalesman());
                msgData.put("customerName", order.getUserName());
                msgData.put("type", AftersalesTypeEnum.getLabelByValue(repair.getType()));
                aliyunSmsClient.sendSmsByType("salesThree", sales.getPhonenumber(), JSON.toJSONString(msgData));
            }
        } catch (Exception e) {
            log.error("预约售后短信发送销售失败！{}",e.getMessage(),e);
        }
    }
    /**
     * 售后分配工单发送通知
     * @param install
     */
    @Async("asyncExecutor")
    public void installAssignWorkerSendNotice(Install install){
        ContractOrder order = orderService.selectContractOrderById(install.getOrderId());
        SysUser worker = getSysUser(install.getWorkerId());
        if(worker==null){
            return;
        }
        try {
            //短信通知客户
            log.info("指派师傅短信发送客户！{}",order.getMobile());
            JSONObject msgData=new JSONObject();
            msgData.put("name",worker.getNickName());
            aliyunSmsClient.sendSmsByType("customerOne",order.getMobile(), JSON.toJSONString(msgData));
        } catch (Exception e) {
            log.error("指派师傅短信发送客户失败！{}",e.getMessage(),e);
        }

        try {
            //短信通知销售

            SysUser sales = getSysUser(order.getSalesmanId());
            if(sales!=null) {
                log.info("指派师傅短信发送销售！{}",sales.getPhonenumber());
                JSONObject msgData = new JSONObject();
                msgData.put("name", order.getUserName());
                aliyunSmsClient.sendSmsByType("salesOne", sales.getPhonenumber(), JSON.toJSONString(msgData));
            }
        } catch (Exception e) {
            log.error("指派师傅短信发送销售失败！{}",e.getMessage(),e);
        }

        try {
            //短信通知师傅
            log.info("指派师傅短信发送师傅！{}",worker.getPhonenumber());
            JSONObject msgData=new JSONObject();
            msgData.put("worker",worker.getNickName());
            msgData.put("type",AftersalesTypeEnum.getLabelByValue(install.getType()));
            msgData.put("customer",order.getUserName());
            msgData.put("address",install.getAddress());
            aliyunSmsClient.sendSmsByType("workerOne",worker.getPhonenumber(), JSON.toJSONString(msgData));
        } catch (Exception e) {
            log.error("指派师傅短信发送师傅失败！{}",e.getMessage(),e);
        }


    }
    /**
     * 售后分配工单发送通知
     * @param repair
     */
    @Async("asyncExecutor")
    public void repairAssignWorkerSendNotice(Repair repair){
        ContractOrder order = orderService.selectContractOrderById(repair.getOrderId());
        SysUser worker = getSysUser(repair.getWorkerId());
        if(worker==null){
            return;
        }
        try {
            //短信通知客户
            log.info("指派师傅短信发送客户！{}",order.getMobile());
            JSONObject msgData=new JSONObject();
            msgData.put("name",worker.getNickName());
            aliyunSmsClient.sendSmsByType("customerOne",order.getMobile(), JSON.toJSONString(msgData));
        } catch (Exception e) {
            log.error("指派师傅短信发送客户失败！{}",e.getMessage(),e);
        }

        try {
            SysUser sales = getSysUser(order.getSalesmanId());
            if(sales!=null){
                //短信通知销售
                log.info("指派师傅短信发送销售！{}",sales.getPhonenumber());
                JSONObject msgData=new JSONObject();
                msgData.put("name",order.getUserName());
                aliyunSmsClient.sendSmsByType("salesOne",sales.getPhonenumber(), JSON.toJSONString(msgData));
            }
        } catch (Exception e) {
            log.error("指派师傅短信发送客户失败！{}",e.getMessage(),e);
        }

        try {
            //短信通知师傅
            log.info("指派师傅短信发送师傅！{}",worker.getPhonenumber());
            JSONObject msgData=new JSONObject();
            msgData.put("worker",worker.getNickName());
            msgData.put("type",AftersalesTypeEnum.getLabelByValue(repair.getType()));
            msgData.put("customer",order.getUserName());
            msgData.put("address",repair.getAddress());
            aliyunSmsClient.sendSmsByType("workerOne",worker.getPhonenumber(), JSON.toJSONString(msgData));
        } catch (Exception e) {
            log.error("指派师傅短信发送客户失败！{}",e.getMessage(),e);
        }
    }
    /**
     * 售后完成发送通知
     * @param repair
     */
    @Async("asyncExecutor")
    public void repairCompletedSendNotice(Repair repair){
        ContractOrder order = orderService.selectContractOrderById(repair.getOrderId());
        try {
            //短信通知客户
            log.info("售后完成短信发送客户！{}",order.getMobile());
            JSONObject msgData=new JSONObject();
            msgData.put("type",AftersalesTypeEnum.getLabelByValue(repair.getType()));
            aliyunSmsClient.sendSmsByType("customerTwo",order.getMobile(), JSON.toJSONString(msgData));
        } catch (Exception e) {
            log.error("售后完成短信发送客户失败！{}",e.getMessage(),e);
        }
        try {
            //短信通知销售
            SysUser sales = getSysUser(order.getSalesmanId());
            if(sales!=null) {
                log.info("售后完成短信发送销售！{}",sales.getPhonenumber());
                JSONObject msgData = new JSONObject();
                msgData.put("type", AftersalesTypeEnum.getLabelByValue(repair.getType()));
                aliyunSmsClient.sendSmsByType("salesTwo", sales.getPhonenumber(), JSON.toJSONString(msgData));
            }
        } catch (Exception e) {
            log.error("售后完成短信发送客户失败！{}",e.getMessage(),e);
        }

    }
    /**
     * 安装完成发送通知
     * @param install
     */
    @Async("asyncExecutor")
    public void installCompletedSendNotice(Install install){
        ContractOrder order = orderService.selectContractOrderById(install.getOrderId());
        try {
            //短信通知客户
            log.info("安装完成短信发送客户！{}",order.getMobile());
            JSONObject msgData=new JSONObject();
            msgData.put("type",AftersalesTypeEnum.getLabelByValue(install.getType()));
            aliyunSmsClient.sendSmsByType("customerTwo",order.getMobile(), JSON.toJSONString(msgData));
        } catch (Exception e) {
            log.error("安装完成短信发送客户失败！{}",e.getMessage(),e);
        }
        try {
            //短信通知销售
            SysUser sales = getSysUser(order.getSalesmanId());
            if(sales!=null) {
                log.info("安装完成短信发送销售！{}",sales.getPhonenumber());
                JSONObject msgData = new JSONObject();
                msgData.put("type", AftersalesTypeEnum.getLabelByValue(install.getType()));
                aliyunSmsClient.sendSmsByType("salesTwo", sales.getPhonenumber(), JSON.toJSONString(msgData));
            }
        } catch (Exception e) {
            log.error("安装完成短信发送客户失败！{}",e.getMessage(),e);
        }
    }
    /**
     * 订单发货发送通知
     * @param order
     */
    @Async("asyncExecutor")
    public void orderSendNotice(ContractOrder order){
        try {
            //短信通知客户
            log.info("订单发货短信发送客户！{}",order.getMobile());
            JSONObject msgData=new JSONObject();
            aliyunSmsClient.sendSmsByType("customerThree",order.getMobile(), JSON.toJSONString(msgData));
        } catch (Exception e) {
            log.error("订单发货短信发送客户失败！{}",e.getMessage(),e);
        }
    }
    /**
     * 订单维保到期发送通知
     * @param order
     */
    @Async("asyncExecutor")
    public void orderEndDateSendNotice(ContractOrder order){

        try {
            //短信通知客户
            log.info("订单维保到期短信发送客户！{}",order.getMobile());
            JSONObject msgData=new JSONObject();
            aliyunSmsClient.sendSmsByType("customerFour",order.getMobile(), JSON.toJSONString(msgData));
        } catch (Exception e) {
            log.error("订单维保到期短信发送客户失败！{}",e.getMessage(),e);
        }
    }

    private SysUser getSysUser(Long userId){
        return sysUserService.selectUserById(userId);
    }

}
